package com.mc.algorithm;

public class FourtyTwo {
	
	public static void foo(String str){
		StringBuilder sb = new StringBuilder(str);
		reverse(sb, 0, sb.length()-1);
		int start = 0;
		int end = 0;
		while(start<sb.length()){
			if(sb.charAt(start)==' '){
				start++;
				end++;
			}
			if(end==sb.length()||sb.charAt(end)==' '){
				reverse(sb, start, end-1);
				start = ++end;
			}
			else{
				end++;
			}
		}
		System.out.println(sb.toString());
	}
	private static void reverse(StringBuilder sb,int start,int end){
		while(start<end){
			char temp = sb.charAt(start);
			sb.setCharAt(start, sb.charAt(end));
			sb.setCharAt(end, temp);
			start++;
			end--;
		}
	}
	public static void main(String[] args) {
		
		FourtyTwo.foo("hello world!");
	}
}
